/*================================================================
*   Copyright (C) 2024 baichao All rights reserved.
*   
*   文件名称：main.c
*   创 建 者：baichao
*   创建日期：2024年08月26日
*   描    述：
*
================================================================*/

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>

#define NUM_THREADS 8  // 根据你的CPU核心数设置线程数量

void *burn_cpu(void *threadid) {
    long tid = (long)threadid;
    printf("Thread #%ld is running...\n", tid);
    while(1) {
        // CPU will continuously perform operations here, keeping it busy
        double x = 0.01;
        for (int i = 0; i < 1000000; i++) {
            x += x * x;
        }
    }
    pthread_exit(NULL);
}

int main(int argc, char *argv[]) {
    pthread_t threads[NUM_THREADS];
    int rc;
    long t;
    
    for (t = 0; t < NUM_THREADS; t++) {
        rc = pthread_create(&threads[t], NULL, burn_cpu, (void *)t);
        if (rc) {
            printf("ERROR: return code from pthread_create() is %d\n", rc);
            exit(-1);
        }
    }
    
    // Wait for all threads to finish (they actually won't, but this keeps the program running)
    for (t = 0; t < NUM_THREADS; t++) {
        pthread_join(threads[t], NULL);
    }
    
    return 0;
}

